<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QMutex Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QMutex Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QMutex class provides access serialization between threads.
<a href="#details">More...</a></p>

<h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qmutex.html#RecursionMode-enum">RecursionMode</a></b> { NonRecursive, Recursive }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qmutex.html#QMutex">__init__</a></b> (<i>self</i>, RecursionMode&#160;<i>mode</i>&#160;=&#160;QMutex.NonRecursive)</li><li><div class="fn" /><b><a href="qmutex.html#lock">lock</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qmutex.html#tryLock">tryLock</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qmutex.html#tryLock-2">tryLock</a></b> (<i>self</i>, int&#160;<i>timeout</i>)</li><li><div class="fn" /><b><a href="qmutex.html#unlock">unlock</a></b> (<i>self</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QMutex class provides access serialization between
threads.</p>
<p>The purpose of a QMutex is to protect an object, data structure
or section of code so that only one thread can access it at a time
(this is similar to the Java <tt>synchronized</tt> keyword). It is
usually best to use a mutex with a <a href="qmutexlocker.html">QMutexLocker</a> since this makes it easy to
ensure that locking and unlocking are performed consistently.</p>
<p>For example, say there is a method that prints a message to the
user on two lines:</p>
<pre class="cpp">
 <span class="type">int</span> number <span class="operator">=</span> <span class="number">6</span>;

 <span class="type">void</span> method1()
 {
     number <span class="operator">*</span><span class="operator">=</span> <span class="number">5</span>;
     number <span class="operator">/</span><span class="operator">=</span> <span class="number">4</span>;
 }

 <span class="type">void</span> method2()
 {
     number <span class="operator">*</span><span class="operator">=</span> <span class="number">3</span>;
     number <span class="operator">/</span><span class="operator">=</span> <span class="number">2</span>;
 }
</pre>
<p>If these two methods are called in succession, the following
happens:</p>
<pre class="cpp">
 <span class="comment">// method1()</span>
 number <span class="operator">*</span><span class="operator">=</span> <span class="number">5</span>;        <span class="comment">// number is now 30</span>
 number <span class="operator">/</span><span class="operator">=</span> <span class="number">4</span>;        <span class="comment">// number is now 7</span>

 <span class="comment">// method2()</span>
 number <span class="operator">*</span><span class="operator">=</span> <span class="number">3</span>;        <span class="comment">// number is now 21</span>
 number <span class="operator">/</span><span class="operator">=</span> <span class="number">2</span>;        <span class="comment">// number is now 10</span>
</pre>
<p>If these two methods are called simultaneously from two threads
then the following sequence could result:</p>
<pre class="cpp">
 <span class="comment">// Thread 1 calls method1()</span>
 number <span class="operator">*</span><span class="operator">=</span> <span class="number">5</span>;        <span class="comment">// number is now 30</span>

 <span class="comment">// Thread 2 calls method2().</span>
 <span class="comment">//</span>
 <span class="comment">// Most likely Thread 1 has been put to sleep by the operating</span>
 <span class="comment">// system to allow Thread 2 to run.</span>
 number <span class="operator">*</span><span class="operator">=</span> <span class="number">3</span>;        <span class="comment">// number is now 90</span>
 number <span class="operator">/</span><span class="operator">=</span> <span class="number">2</span>;        <span class="comment">// number is now 45</span>

 <span class="comment">// Thread 1 finishes executing.</span>
 number <span class="operator">/</span><span class="operator">=</span> <span class="number">4</span>;        <span class="comment">// number is now 11, instead of 10</span>
</pre>
<p>If we add a mutex, we should get the result we want:</p>
<pre class="cpp">
 <span class="type">QMutex</span> mutex;
 <span class="type">int</span> number <span class="operator">=</span> <span class="number">6</span>;

 <span class="type">void</span> method1()
 {
     mutex<span class="operator">.</span>lock();
     number <span class="operator">*</span><span class="operator">=</span> <span class="number">5</span>;
     number <span class="operator">/</span><span class="operator">=</span> <span class="number">4</span>;
     mutex<span class="operator">.</span>unlock();
 }

 <span class="type">void</span> method2()
 {
     mutex<span class="operator">.</span>lock();
     number <span class="operator">*</span><span class="operator">=</span> <span class="number">3</span>;
     number <span class="operator">/</span><span class="operator">=</span> <span class="number">2</span>;
     mutex<span class="operator">.</span>unlock();
 }
</pre>
<p>Then only one thread can modify <tt>number</tt> at any given
time and the result is correct. This is a trivial example, of
course, but applies to any other case where things need to happen
in a particular sequence.</p>
<p>When you call <a href="qmutex.html#lock">lock</a>() in a thread,
other threads that try to call <a href="qmutex.html#lock">lock</a>() in the same place will block until
the thread that got the lock calls <a href="qmutex.html#unlock">unlock</a>(). A non-blocking alternative to
<a href="qmutex.html#lock">lock</a>() is <a href="qmutex.html#tryLock">tryLock</a>().</p>
<hr /><h2>Type Documentation</h2><h3 class="fn"><a name="RecursionMode-enum" />QMutex.RecursionMode</h3><table class="valuelist">
<tr class="odd" valign="top">
<th class="tblConst">Constant</th>
<th class="tblval">Value</th>
<th class="tbldscr">Description</th>
</tr>
<tr>
<td class="topAlign"><tt>QMutex.Recursive</tt></td>
<td class="topAlign"><tt>1</tt></td>
<td class="topAlign">In this mode, a thread can lock the same mutex
multiple times and the mutex won't be unlocked until a
corresponding number of <a href="qmutex.html#unlock">unlock</a>()
calls have been made.</td>
</tr>
<tr>
<td class="topAlign"><tt>QMutex.NonRecursive</tt></td>
<td class="topAlign"><tt>0</tt></td>
<td class="topAlign">In this mode, a thread may only lock a mutex
once.</td>
</tr>
</table>
<p><b>See also</b> <a href="qmutex.html#QMutex">QMutex</a>().</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QMutex" />QMutex.__init__ (<i>self</i>, <a href="qmutex.html#RecursionMode-enum">RecursionMode</a>&#160;<i>mode</i>&#160;=&#160;QMutex.NonRecursive)</h3><p>Constructs a new mutex. The mutex is created in an unlocked
state.</p>
<p>If <i>mode</i> is <a href="qmutex.html#RecursionMode-enum">QMutex.Recursive</a>, a thread
can lock the same mutex multiple times and the mutex won't be
unlocked until a corresponding number of <a href="qmutex.html#unlock">unlock</a>() calls have been made. The default
is <a href="qmutex.html#RecursionMode-enum">QMutex.NonRecursive</a>.</p>
<p><b>See also</b> <a href="qmutex.html#lock">lock</a>() and
<a href="qmutex.html#unlock">unlock</a>().</p>


<h3 class="fn"><a name="lock" />QMutex.lock (<i>self</i>)</h3><p>Locks the mutex. If another thread has locked the mutex then
this call will block until that thread has unlocked it.</p>
<p>Calling this function multiple times on the same mutex from the
same thread is allowed if this mutex is a <a href="qmutex.html#RecursionMode-enum">recursive mutex</a>. If this mutex
is a <a href="qmutex.html#RecursionMode-enum">non-recursive
mutex</a>, this function will <i>dead-lock</i> when the mutex is
locked recursively.</p>
<p><b>See also</b> <a href="qmutex.html#unlock">unlock</a>().</p>


<h3 class="fn"><a name="tryLock" />bool QMutex.tryLock (<i>self</i>)</h3><p>Attempts to lock the mutex. If the lock was obtained, this
function returns true. If another thread has locked the mutex, this
function returns false immediately.</p>
<p>If the lock was obtained, the mutex must be unlocked with
<a href="qmutex.html#unlock">unlock</a>() before another thread can
successfully lock it.</p>
<p>Calling this function multiple times on the same mutex from the
same thread is allowed if this mutex is a <a href="qmutex.html#RecursionMode-enum">recursive mutex</a>. If this mutex
is a <a href="qmutex.html#RecursionMode-enum">non-recursive
mutex</a>, this function will <i>always</i> return false when
attempting to lock the mutex recursively.</p>
<p><b>See also</b> <a href="qmutex.html#lock">lock</a>() and
<a href="qmutex.html#unlock">unlock</a>().</p>


<h3 class="fn"><a name="tryLock-2" />bool QMutex.tryLock (<i>self</i>, int&#160;<i>timeout</i>)</h3><p>This is an overloaded function.</p>
<p>Attempts to lock the mutex. This function returns true if the
lock was obtained; otherwise it returns false. If another thread
has locked the mutex, this function will wait for at most
<i>timeout</i> milliseconds for the mutex to become available.</p>
<p>Note: Passing a negative number as the <i>timeout</i> is
equivalent to calling <a href="qmutex.html#lock">lock</a>(), i.e.
this function will wait forever until mutex can be locked if
<i>timeout</i> is negative.</p>
<p>If the lock was obtained, the mutex must be unlocked with
<a href="qmutex.html#unlock">unlock</a>() before another thread can
successfully lock it.</p>
<p>Calling this function multiple times on the same mutex from the
same thread is allowed if this mutex is a <a href="qmutex.html#RecursionMode-enum">recursive mutex</a>. If this mutex
is a <a href="qmutex.html#RecursionMode-enum">non-recursive
mutex</a>, this function will <i>always</i> return false when
attempting to lock the mutex recursively.</p>
<p><b>See also</b> <a href="qmutex.html#lock">lock</a>() and
<a href="qmutex.html#unlock">unlock</a>().</p>


<h3 class="fn"><a name="unlock" />QMutex.unlock (<i>self</i>)</h3><p>Unlocks the mutex. Attempting to unlock a mutex in a different
thread to the one that locked it results in an error. Unlocking a
mutex that is not locked results in undefined behavior.</p>
<p><b>See also</b> <a href="qmutex.html#lock">lock</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;snapshot-4.9.5-9eb6aac99275 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt&#160;4.8.3</td></tr></table></div></address></body></html>